﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CCWpf
{
    /// <summary>
    /// MsgBoxWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MsgBoxWindow : Window
    {
        public MsgBoxWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            OkBtnVisibility = Visibility.Collapsed;
            YesBtnVisibility = Visibility.Collapsed;
            NoBtnVisibility = Visibility.Collapsed;
            CancelBtnVisibility = Visibility.Collapsed;
            MsgResult = CustomMsgBoxResult.None;
        }
        /// <summary>
        /// 显示按钮类型
        /// </summary>
        public enum CustomMsgBoxButton
        {
            Ok = 1,
            OkCancel = 2,
            YesNo = 3,
            YesNoCancel = 4,
        }
        /// <summary>
        /// 消息框的返回类型
        /// </summary>
        public enum CustomMsgBoxResult
        {
            None = 0,
            Ok = 1,
            Cancel = 2,
            Yes = 3,
            No = 4,
        }
        /// <summary>
        /// 图标类型
        /// </summary>
        public enum CustomMsgBoxIcon
        {
            None = 0,
            Error = 1,
            Question = 2,
            Infomation = 3,
        }
        //页面属性定义 MsgTitle/MsgContent/ImagePath/OkBtnVisibility/CancelBtnVisibility/YesBtnVisibility/NoBtnVisibility/MsgResult
        /// <summary>
        /// 标题
        /// </summary>
        public string MsgTitle { get; set; }
        /// <summary>
        /// 消息文本
        /// </summary>
        public string MsgContent { get; set; }
        /// <summary>
        /// 图标路径
        /// </summary>
        public string ImagePath { get; set; }
        /// <summary>
        /// 按钮显示
        /// </summary>
        public Visibility OkBtnVisibility { get; set; }
        public Visibility CancelBtnVisibility { get; set; }
        public Visibility YesBtnVisibility { get; set; }
        public Visibility NoBtnVisibility { get; set; }
        /// <summary>
        /// 消息框返回结果属性
        /// </summary>
        public CustomMsgBoxResult MsgResult { get; set; }

        /// <summary>
        /// 窗口拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
        /// <summary>
        /// 点击右上角关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }
        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            MsgResult = CustomMsgBoxResult.Ok;
            this.Close();
        }
        private void yesButton_Click(object sender, RoutedEventArgs e)
        {
            MsgResult = CustomMsgBoxResult.Yes;
            this.Close();
        }
        private void noButton_Click(object sender, RoutedEventArgs e)
        {
            MsgResult = CustomMsgBoxResult.No;
            this.Close();
        }
        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            MsgResult = CustomMsgBoxResult.Cancel;
            this.Close();
        }
        /// <summary>
        /// 显示消息框方法
        /// </summary>
        /// <param name="title"></param>
        /// <param name="msgText"></param>
        /// <param name="msgBtn"></param>
        /// <param name="msgIcon"></param>
        /// <returns></returns>
        public static CustomMsgBoxResult Show(string title, string msgText, CustomMsgBoxButton msgBtn,CustomMsgBoxIcon msgIcon)
        { 
            //创建一个窗口对象，信息设置-标题，文本，按钮，图标
            MsgBoxWindow msgWin= new MsgBoxWindow();
            msgWin.Topmost = true;
            msgWin.MsgTitle = title;
            msgWin.MsgContent = msgText;
            //按钮的显示
            switch (msgBtn)
            { 
                case CustomMsgBoxButton.Ok:
                    msgWin.OkBtnVisibility=Visibility.Visible; 
                    break;
                case CustomMsgBoxButton.OkCancel:
                    msgWin.OkBtnVisibility = Visibility.Visible;
                    msgWin.CancelBtnVisibility = Visibility.Visible;
                    break;
                case CustomMsgBoxButton.YesNo:
                    msgWin.YesBtnVisibility = Visibility.Visible;
                    msgWin.NoBtnVisibility = Visibility.Visible;
                    break;
                case CustomMsgBoxButton.YesNoCancel:
                    msgWin.YesBtnVisibility = Visibility.Visible;
                    msgWin.NoBtnVisibility = Visibility.Visible;
                    msgWin.CancelBtnVisibility = Visibility.Visible;
                    break;
                default:
                    msgWin.OkBtnVisibility = Visibility.Visible;
                    break;
            }
            //图标的显示
            switch (msgIcon)
            {
                case CustomMsgBoxIcon.Infomation:
                    msgWin.ImagePath = @"imgs/sucess.png";
                    break;
                case CustomMsgBoxIcon.Error:
                    msgWin.ImagePath = @"imgs/error.png";
                    break; 
                case CustomMsgBoxIcon.Question:
                    msgWin.ImagePath = @"imgs/question.png";
                    break;
            }
            msgWin.ShowDialog();
            return msgWin.MsgResult;
        }
    }
}
